﻿using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.FIM;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Linq.Dynamic;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Web.Mvc;
using KPIS.GERP.Models.SYS;

namespace KPIS.GERP.WCF.GFMS
{
    [ServiceContract]
    public interface IChequeBookService
    {
        [OperationContract]
        IEnumerable<KPIS.GERP.Models.FIM.Cheque> SelectCheque(Cheque criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertCheque(Cheque model, int createByUserId);

        [OperationContract]
        void UpdateCheque(Cheque model, int updateByUserId);

        [OperationContract]
        void DeleteCheque(int id, int deleteByUserId);

        [OperationContract]
        List<SelectListItem> SelectListBankType(int? bankAccId);

        [OperationContract]
        IEnumerable<KPIS.GERP.Models.FIM.ChequeBook> SelectChequeBook(ChequeBook criteriaModel = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int InsertChequeBook(ChequeBook model, int createByUserId);

        [OperationContract]
        void UpdateChequeBook(ChequeBook model, int updateByUserId);

        [OperationContract]
        void DeleteChequeBook(int id, int deleteByUserId);
    }

    [ServiceErrorBehavior(typeof(ElmahErrorHandler))]
    public class ChequeBookService : IChequeBookService
    {
        GERPContext db = new GERPContext();

        #region Cheque
        public IEnumerable<KPIS.GERP.Models.FIM.Cheque> SelectCheque(Cheque criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<Cheque> query = context.Cheques;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertCheque(Cheque model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {

                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.Cheques.Add(model);
                    context.SaveChanges();

                    return model.ChequeId;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateCheque(Cheque model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteCheque(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.Cheques.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.ChequeStatus = "1";
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public List<SelectListItem> SelectListBankType(int? bankAccId)
        {
            var list = new List<SelectListItem>();

            try
            {
                List<Models.FIM.MasBankAccount> listBankAccount = (from m in db.MasBankAccounts
                                                            where m.RecordStatus != Models.SYS.RecordStatus.DeletedStatus
                                                            && m.BankAccTypeId == 2
                                                            select m).ToList();

                foreach (var BankAccount in listBankAccount)
                {
                    var item = new SelectListItem { Text = BankAccount.Name, Value = BankAccount.BankAccId.ToString() };
                    if (BankAccount.BankAccId == bankAccId) item.Selected = true;
                    list.Add(item);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return list;
            
        }

        #endregion


        #region ChequeBook
        public IEnumerable<KPIS.GERP.Models.FIM.ChequeBook> SelectChequeBook(ChequeBook criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<ChequeBook> query = context.ChequeBooks;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    return query.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public int InsertChequeBook(ChequeBook model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {

                    model.RecordStatus = RecordStatus.UsedStatus;
                    model.CreatedBy = createByUserId;
                    model.CreatedWhen = DateTime.Now;
                    context.ChequeBooks.Add(model);
                    context.SaveChanges();

                    return model.ChequeBookId;

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void UpdateChequeBook(ChequeBook model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    model.UpdatedBy = updateByUserId;
                    model.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void DeleteChequeBook(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.ChequeBooks.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.ChequeBookStatus = "1";
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    modelToDelete.UpdatedBy = deleteByUserId;
                    modelToDelete.UpdatedWhen = DateTime.Now;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
